// set function parseTime,formatTime to filter
export { parseTime, formatTime } from '@/utils'
import store from '@/store'

function pluralize(time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}

export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

/* 数字 格式化*/
export function numberFormatter(num, digits) {
  const si = [
    { value: 1E18, symbol: 'E' },
    { value: 1E15, symbol: 'P' },
    { value: 1E12, symbol: 'T' },
    { value: 1E9, symbol: 'G' },
    { value: 1E6, symbol: 'M' },
    { value: 1E3, symbol: 'k' }
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value + 0.1).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
    }
  }
  return num.toString()
}

export function toThousandFilter(num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}

// export function toDate(time){
//   var date = new Date(time*1000 + 8 * 3600 * 1000);
//   return date.toJSON().substr(0, 10).replace('T', ' ').replace(/-/g, '-');
// }

// export function toTime(time){
//     var date = new Date(time*1000 + 8 * 3600 * 1000);
//     return date.toJSON().toString().slice(11,16);
// }

// export function toDateTime(time){
//   var date = new Date(time*1000 + 8 * 3600 * 1000);
//   return date.toJSON().substr(0, 16).replace('T', ' ').replace(/-/g, '-');
// }

export function toTimes(time) {
  return time.toString().substring(0, 5)
}
export function toDate(time) {
  return time.substring(0, 10)
}
export function toTime(time) {
  return time.substring(10, 16)
}

export function toTemp(temp) {
  if (temp) {
    if (store.getters.is_centigrade == '2') {
      return ((temp * 9) / 5 + 32).toFixed(2) + '℉'
    } else {
      return temp.toFixed(2) + '℃'
    }
  }
}
